if (!file.exists("results-2023-08-04.RData")) {
  library(rstan)
  library(rstanarm)
  library(e1071)
  library(data.table)
  source("functions.R")
  rstan_options(auto_write = TRUE)
  options(mc.cores = parallel::detectCores())
  RNGkind("L'Ecuyer-CMRG")
  set.seed(1699841150)
  load("analysis_data.RData")

  # Table 2
  m_eqm_action_lo <- stan_lmer(eqm_action_proximity ~ condition +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data[bias == "Low"], adapt_delta = .99)
  m_eqm_action_hi <- stan_lmer(eqm_action_proximity ~ condition +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data[bias == "High"], adapt_delta = .99)

  # Table 3
  m_comp_stat_numeric <- stan_lmer(target_action_proximity ~ bias +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data[condition == "Numeric"], adapt_delta = .99)
  m_comp_stat_text <- stan_lmer(target_action_proximity ~ bias +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data[condition == "Text"], adapt_delta = .99)

  # Table 4
  m_payoff_sender <- stan_lmer(sender_payoff ~ condition * bias +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data, adapt_delta = .99)
  m_payoff_receiver <- stan_lmer(receiver_payoff ~ condition * bias +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data, adapt_delta = .99)
  m_pareto <- stan_lmer(pareto ~ condition * bias +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data, adapt_delta = .99)

  # Table 5
  m1 <- stan_glmer(1*(category == "Empty") ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m2 <- stan_glmer(1*(category == "Precise") ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m3 <- stan_glmer(1*(category == "Interval") ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m4 <- stan_glmer(1*(category == "Noisy") ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m5 <- stan_glmer(honesty ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m6 <- stan_glmer(fairness ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)

  # Table 6
  m_mention_r_target <- stan_lmer(
    mention_r_target ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m_mention_s_target <- stan_lmer(
    mention_s_target ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m_mention_action <- stan_lmer(
    mention_action ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)
  m_mention_midpoint <- stan_lmer(
    Midpoint ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages, adapt_delta = .99)

  # Table 7
  m_target_message_proximity <- stan_lmer(
    target_message_proximity ~ bias * condition +
      (1 | session) + (1 | sender_id) + (1 | period),
    data = pair_data, adapt_delta = .99)
  m_message_target <- stan_lmer(
    message_num ~ target * bias * condition +
      (1 | session) + (1 | sender_id) + (1 | period),
    data = pair_data, adapt_delta = .99)

  # Figure 2
  X <- merge(text_messages,
    pair_data[condition == "Text"],
    by.x = c("Bias", "Target", "id", "time"),
    by.y = c("bias", "target", "sender_id", "time"))
  X[, Empty := as.numeric(category == "Empty")]
  X[, Precise := as.numeric(category == "Precise")]
  X[, Noisy := as.numeric(category == "Noisy")]
  X[, Interval := as.numeric(category == "Interval")]
  fmla <- ~
    Empty + Precise + Interval + Noisy + honesty + fairness +
    mention_s_target + mention_r_target + mention_action + Midpoint +
    target.01 + time + Bias
  sender_payoff_svm <- svm(update(fmla, sender_payoff ~ .), data = X)
  receiver_payoff_svm <- svm(update(fmla, receiver_payoff ~ .), data = X)
  pareto_svm <- svm(update(fmla, pareto ~ .), data = X)
  sender_payoff_effects <- rbind(
    calc_effect(sender_payoff_svm, "Empty", "Empty"),
    calc_effect(sender_payoff_svm, "Precise", "Precise"),
    calc_effect(sender_payoff_svm, "Interval", "Interval"),
    calc_effect(sender_payoff_svm, "Noisy", "Noisy"),
    calc_effect(sender_payoff_svm, "honesty", "Honesty"),
    calc_effect(sender_payoff_svm, "fairness", "Fairness"),
    calc_effect(sender_payoff_svm, "mention_s_target", "Mention\nS's Target"),
    calc_effect(sender_payoff_svm, "mention_r_target", "Mention\nR's Target"),
    calc_effect(sender_payoff_svm, "mention_action", "Mention\nAction"),
    calc_effect(sender_payoff_svm, "Midpoint", "Mention\nMidpoint")
  )
  levels <- sender_payoff_effects[order(estimate), variable]
  sender_payoff_effects[, variable := factor(variable, levels)]
  receiver_payoff_effects <- rbind(
    calc_effect(receiver_payoff_svm, "Empty", "Empty"),
    calc_effect(receiver_payoff_svm, "Precise", "Precise"),
    calc_effect(receiver_payoff_svm, "Interval", "Interval"),
    calc_effect(receiver_payoff_svm, "Noisy", "Noisy"),
    calc_effect(receiver_payoff_svm, "honesty", "Honesty"),
    calc_effect(receiver_payoff_svm, "fairness", "Fairness"),
    calc_effect(receiver_payoff_svm, "mention_s_target", "Mention\nS's Target"),
    calc_effect(receiver_payoff_svm, "mention_r_target", "Mention\nR's Target"),
    calc_effect(receiver_payoff_svm, "mention_action", "Mention\nAction"),
    calc_effect(receiver_payoff_svm, "Midpoint", "Mention\nMidpoint")
  )
  levels <- receiver_payoff_effects[order(estimate), variable]
  receiver_payoff_effects[, variable := factor(variable, levels)]
  pareto_effects <- rbind(
    calc_effect(pareto_svm, "Empty", "Empty"),
    calc_effect(pareto_svm, "Precise", "Precise"),
    calc_effect(pareto_svm, "Interval", "Interval"),
    calc_effect(pareto_svm, "Noisy", "Noisy"),
    calc_effect(pareto_svm, "honesty", "Honesty"),
    calc_effect(pareto_svm, "fairness", "Fairness"),
    calc_effect(pareto_svm, "mention_s_target", "Mention\nS's Target"),
    calc_effect(pareto_svm, "mention_r_target", "Mention\nR's Target"),
    calc_effect(pareto_svm, "mention_action", "Mention\nAction"),
    calc_effect(pareto_svm, "Midpoint", "Mention\nMidpoint")
  )
  levels <- pareto_effects[order(estimate), variable]
  pareto_effects[, variable := factor(variable, levels)]
  save(
    m_comp_stat_numeric, m_comp_stat_text,
    m_eqm_action_lo, m_eqm_action_hi,
    m_payoff_sender, m_payoff_receiver, m_pareto,
    m1, m2, m3, m4, m5, m6,
    m_target_message_proximity, m_message_target,
    m_mention_r_target, m_mention_s_target, m_mention_action, m_mention_midpoint,
    sender_payoff_effects, receiver_payoff_effects, pareto_effects,
    file = "results-2023-08-04.RData")
}


if (!file.exists("results-2023-08-31.RData")) {
  library(rstan)
  library(rstanarm)
  library(data.table)
  source("R/functions.R")
  rstan_options(auto_write = TRUE)
  options(mc.cores = parallel::detectCores())
  RNGkind("L'Ecuyer-CMRG")
  set.seed(1805648849)
  load("analysis_data.RData")

  # Table 3, robustness check
  pair_data[, target_below_cutoff := as.numeric(bias == "Low" & target < -80)]
  pair_data[, .N, .(bias, target_below_cutoff)]
  m_comp_stat_numeric2 <- stan_lmer(target_action_proximity ~ bias +
      target_below_cutoff +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data[condition == "Numeric"], adapt_delta = .99)
  m_comp_stat_text2 <- stan_lmer(target_action_proximity ~ bias +
      target_below_cutoff +
      (1 | session) + (1 | period) + (1 | sender_id) + (1 | receiver_id),
    data = pair_data[condition == "Text"], adapt_delta = .99)
  save(
    m_comp_stat_numeric2, m_comp_stat_text2,
    file = "results-2023-08-31.RData")

}

if (!file.exists("results-2024-03-07.RData")) {
  library(rstan)
  library(rstanarm)
  library(e1071)
  library(data.table)
  source("functions.R")
  rstan_options(auto_write = TRUE)
  options(mc.cores = parallel::detectCores())
  RNGkind("L'Ecuyer-CMRG")
  set.seed(1699841150)
  load("analysis_data.RData")

  m2_r_less_than_60 <- stan_glmer(1*(category == "Precise") ~ 
      target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages[Target < 60], adapt_delta = .99)

  m_mention_r_target_r_less_than_60 <- stan_lmer(
    mention_r_target ~ target.01 + time + Bias +
      (1 | Session) + (1 | id) + (1 | Period),
    data = text_messages[Target < 60], adapt_delta = .99)
  save(
    m2_r_less_than_60, m_mention_r_target_r_less_than_60,
    file = "results-2024-03-07.RData")
}